package gen.background.util;

import java.io.File;
import java.net.URL;

import gen.background.TemplateParse;

public class DefaultPathAnalysis implements PathAnalysis{

	@Override
	public String PathAnalysis(String uri) {
		URL url = DefaultPathAnalysis.class.getResource(uri);
		if(url == null){
			if(uri.lastIndexOf("/") == -1){
				if(uri.indexOf(":") == -1){
					return uri;
				}
				return DefaultPathAnalysis.class.getResource("/").getPath().replace("/bin/", "/src/")+uri;
			}
			uri = dirGenerate(
					uri.substring(0, uri.lastIndexOf("/")))+uri.substring(uri.lastIndexOf("/"));
		}else{
			uri = url.getPath();
		}
		return uri;
	}
	
	private String dirGenerate(String dir){
		URL uur = DefaultPathAnalysis.class.getResource(dir);
		if(uur == null){
			String dirName = dir;
			if(dir.lastIndexOf("/") != -1){
				String parentDir = dir.substring(0, dir.lastIndexOf("/"));
				dirName = dir.substring(dir.lastIndexOf("/"));
				dirName = dirGenerate(parentDir)+dirName;
			}else{
				if(dir.indexOf(":") == -1){
					dirName = DefaultPathAnalysis.class.getResource("/").getPath().replace("/bin/", "/src/")+dir;
				}
			}
			new File(dirName).mkdirs();
			return dirName;
		}
		return uur.getPath();
	}

}
